package com.software.levelupandroid.mine.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.software.levelupandroid.R;
import com.software.levelupandroid.entity.Badge;

import java.util.List;

/**
 * @Description TODO
 * @Author LiangYaJie
 * @CreateTime 2024年06月11日 19:33:33
 */
public class BadgeAdapter extends RecyclerView.Adapter<BadgeAdapter.ViewHolder> {

    private List<Badge> allBadges;
    private List<Badge> ownBadges;
    private Context context;
    private static final int GRADE_12 = 12;
    private static final int GRADE_34 = 34;
    private static final int GRADE_56 = 56;
    private static final int GRADE_7 = 7;

    @Override
    public int getItemViewType(int position) {
        Badge badge = allBadges.get(position);
        if (badge.getBadgeGrade() == 1 || badge.getBadgeGrade() ==2){
            return GRADE_12;
        }else if (badge.getBadgeGrade() == 3 || badge.getBadgeGrade() ==4){
            return GRADE_34;
        }else if (badge.getBadgeGrade() == 5 || badge.getBadgeGrade() ==6){
            return GRADE_56;
        }else {
            return GRADE_7;
        }
    }

    public BadgeAdapter(List<Badge> allBadges, List<Badge> ownBadges, Context context) {
        this.allBadges = allBadges;
        this.ownBadges = ownBadges;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == GRADE_12){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_badge_12, parent, false);
        }else if (viewType == GRADE_34){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_badge_34, parent, false);
        }else if (viewType == GRADE_56){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_badge_56, parent, false);
        }else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_badge_7, parent, false);
        }

        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Badge badge = allBadges.get(position);
        Badge ownBadge = null;
        if (position < ownBadges.size())
            ownBadge = ownBadges.get(position);

        if (ownBadge != null)
            holder.rlBadgeBackGround.setBackgroundColor(ContextCompat.getColor(context, R.color.blue_hex25));

        int badgeType = badge.getBadgeType();
        switch (badgeType){
            case 1:
                holder.tvBadgeType.setText("学习");
                break;
            case 2:
                holder.tvBadgeType.setText("生活");
                break;
            case 3:
                holder.tvBadgeType.setText("运动");
                break;
            case 4:
                holder.tvBadgeType.setText("情感");
                break;
            default:
                break;
        }

        holder.tvBadgeName.setText(badge.getBadgeName());

    }

    @Override
    public int getItemCount() {
        return allBadges.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout rlBadgeBackGround;
        private ImageView ivBadge;
        private TextView tvBadgeType;
        private TextView tvBadgeName;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            rlBadgeBackGround = itemView.findViewById(R.id.rl_badge_background);
            ivBadge = itemView.findViewById(R.id.iv_badge);
            tvBadgeType = itemView.findViewById(R.id.tv_badge_type);
            tvBadgeName = itemView.findViewById(R.id.tv_badge_name);
        }
    }
}
